package com.banesco.precargadepositos.interfaz.componentes;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.ScrollChangeListener;
import net.rim.device.api.ui.container.VerticalFieldManager;

/**
 * Control que personaliza las propiedades del panel principal
 * @author maguca
 *
 */
public class PanelContenedor extends VerticalFieldManager implements ScrollChangeListener {

	/**
	 * Variable que posee la informacion del ancho
	 */
	private int _deviceWidth = Display.getWidth();
	
	
	/**
	 * Variable que posee la informacion del alto
	 */
	private int _deviceHeight = Display.getHeight();

		
	/**
	 * Constructor de la clase
	 */
	public PanelContenedor(){
		super(Manager.VERTICAL_SCROLL |  Manager.VERTICAL_SCROLLBAR);
	}
	
	
	/**
	 * Metodo que perzonaliza la manera de pintar los controles dentro del 
	 * panel contendor
	 * @param maxWidth es el maximo ancho del panel
	 * @param maxHeight es el maximo alto del panel
	 */
    protected void sublayout(int maxWidth, int maxHeight)
    {

        super.sublayout( _deviceWidth, _deviceHeight);
        setExtent( _deviceWidth, _deviceHeight);
    }

	
	private boolean MY_CHANGING_SCROLL = false;
	
	public void scrollChanged(Manager manager, int newHorizontalScroll, int newVerticalScroll){
	  if (!MY_CHANGING_SCROLL){
	    MY_CHANGING_SCROLL = true;
	    invCheckVerticalScroll();
	    MY_CHANGING_SCROLL = false;
	  }      
	}  


	protected int myMaxVerticalScrollPosition(){
	  int vh = getVirtualHeight();
	  int h = getHeight();
	  if (vh < h ) return 0; // no scroll
	  return vh - h; // don't scroll lower than limit.
	}

	
	protected void invCheckVerticalScroll() {
	    int i = getVerticalScroll();
	    int m = myMaxVerticalScrollPosition();
	    if ( i > m){
	        i = m;
	        setVerticalScroll(i);
	    }
	}
}
